package org.sage.sdk.dada;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * auto create clsss file
 *
 * @author 周广明
 * @version v1 2019/12/10 下午3:09
 */
public class Tooles {


    static List<String> classFileNames = new ArrayList<String>();

    public static void main(String[] args) {
        URL resource = Tooles.class.getClassLoader().getResource("8.txt");
        try {
            assert resource != null;
            readInfoFile(resource.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void readInfoFile(String path) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(path));

        String line;
        while ((line = br.readLine()) != null) {
            line = line.replaceAll("\\s*", "");

            // file info
            String[] classInfo = line.split("#");

//            if (classInfo.length != 4) {
//
//                for (String s : classInfo) {
//                    System.out.print(s + " ");
//                }
//                throw new Exception("Wrong type parameter!");
//            }

            // create it.
            // createClassFile(new ClassFileInfo(classInfo[0], classInfo[1], classInfo[2], classInfo[3]));

            String msg = classInfo[1];
            msg = msg.replace("[", "");
            msg = msg.replace("]","");
            msg = msg.replace(",","");
            msg = msg.replace("(","");
            msg = msg.replace(")","");

            String code = classInfo[0];
            code = code.replaceAll("\\s*","");

            createEnum(code, msg);
        }

        System.out.println("\n创建完毕！\n");
        //classFileNames.forEach(item -> System.out.println(item + " " + StrUtil.lowerFirst(item) + ";\n"));

    }

    /**
     * Case: shop_no#String#是#门店编号，门店创建后可在门店列表和单页查看
     *
     * @param classFileInfo
     */
    static void createClassFile(ClassFileInfo classFileInfo) {
        // System.out.println(classFileInfo.toString());


        String remark = "/**\n" +
                " * " + classFileInfo.getRemark() + "\n" +
                " * <p>\n" +
                " * 必填: " + classFileInfo.getEssential() + "\n\n" +
                " * @author SAGE\n" +
                " * @version v1 " + DateUtil.formatDateTime(new Date()) + "\n" +
                " */\n";

        // 驼峰
        String classFileName = StrUtil.toCamelCase(classFileInfo.getName());
        // 首字母大写
        classFileName = StrUtil.upperFirst(classFileName);

        classFileNames.add(classFileName);

        String property = "\n\n    " + classFileInfo.getType() + " val;\n";
        String name = "public class " + classFileName + " {" + property + "}";

        // class info
        String infoStr = remark + name;

        URL resource = Tooles.class.getClassLoader().getResource("");
        assert resource != null;
        String path = resource.getPath();


        String main = "org/sage/sdk/dada/infrastructure/common/attributes/";


        path = path.substring(0, path.indexOf("target")) + "src/main/java/" + main;
        String filePath = path + classFileName + ".java";

        // go
        TextToFile(filePath, infoStr);

    }


    /**
     * ClassFileInfo
     *
     * @author 周广明
     * @version v1 2019/12/10 下午3:36
     */
    @Data
    public static class ClassFileInfo {
        ClassFileInfo(String name, String type, String essential, String remark) {
            this.name = name;
            this.type = type;
            this.essential = essential;
            this.remark = remark;
        }

        String name;
        String type;
        String remark;
        String essential;

    }

    /**
     * save file
     *
     * @param strFilename
     * @param strBuffer
     */
    public static void TextToFile(final String strFilename, final String strBuffer) {
        try {
            // 创建文件对象
            File fileText = new File(strFilename);

            // 如果文件存在就跳过
            if (fileText.exists()) {
                System.out.println("文件存在: " + strFilename);
                return;
            }

            // 向文件写入对象写入信息
            FileWriter fileWriter = new FileWriter(fileText);

            // 写文件
            fileWriter.write(strBuffer);
            // 关闭
            fileWriter.close();
        } catch (IOException e) {
            //
            e.printStackTrace();
        }
    }

    public static boolean createDir(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists()) {
            System.out.println("创建目录" + destDirName + "失败，目标目录已经存在");
            return false;
        }
        if (!destDirName.endsWith(File.separator)) {
            destDirName = destDirName + File.separator;
        }
        //创建目录
        if (dir.mkdirs()) {
            System.out.println("创建目录" + destDirName + "成功！");
            return true;
        } else {
            System.out.println("创建目录" + destDirName + "失败！");
            return false;
        }
    }


    private static void createEnum(String code, String msg) {
        System.out.println(msg + "(" + code + "\t,\"" + msg + "\"),");
    }
}
